<template>
  <div class="container">
    <el-card>
      <el-row style="hight" class="top">
        <el-col>
          <el-button
            type="success"
            icon="el-icon-edit"
            style="float :right"
            @click="$router.push('/questions/new')"
          >新增试题</el-button>
        </el-col>
      </el-row>
      <el-form :rules="rules" ref="form" :model="form" label-width="80px" :inline="true">
        <el-form-item label="学科" prop="subjectName">
          <div class="d-flex flex-grow-1">
            <el-select v-model="form.subjectName" placeholder="请选择" @change="btnClick">
              <el-option
                v-for="item in subjectName"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="二级目录" prop="twoLevelDirectory">
          <div class="d-flex flex-grow-1">
            <el-select
              @change="btnClick2"
              v-model="form.twoLevelDirectory.directoryName"
              placeholder="请选择"
            >
              <el-option
                v-for="item in form.twoLevelDirectory"
                :key="item.id"
                :label="item.directoryName"
                :value="item.directoryName"
              ></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <div class="d-flex flex-grow-1">
            <el-select v-model="form.tags.tagName" placeholder="请选择">
              <el-option
                v-for="item in form.tags"
                :key="item.id"
                :label="item.tagName"
                :value="item.tagName"
              ></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请选择" class="a"></el-input>
        </el-form-item>
        <el-form-item label="试题类型" prop="questionType">
          <div class="d-flex flex-grow-1">
            <el-select v-model="form.questionType" placeholder="请选择">
              <el-option
                v-for="(item,index) in values"
                :key="index"
                :label="item.value"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="难度" prop="difficulty">
          <div class="d-flex flex-grow-1">
            <el-select v-model="form.difficulty" placeholder="请选择">
              <el-option
                v-for="(item,index) in value"
                :key="index"
                :label="item.value"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="方向" prop="direction">
          <div class="d-flex flex-grow-1">
            <el-select v-model="form.direction" placeholder="请选择">
              <el-option
                v-for="(item,index) in directions"
                :key="index"
                :label="item.value"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="录入人" prop="userName">
          <div class="d-flex flex-grow-1">
            <el-select v-model="form.userName" placeholder="请选择">
              <el-option
                v-for="(item,index) in userName"
                :key="index"
                :label="item.username"
                :value="item.username"
              ></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="题目备注" prop="notes">
          <el-input v-model="form.notes" placeholder="请选择"></el-input>
        </el-form-item>
        <el-form-item label="企业简称" prop="enterprise">
          <el-input v-model="form.enterprise " placeholder="请选择"></el-input>
        </el-form-item>
        <el-form-item label="城市" class="direction" prop="provinces">
          <el-select
            v-model="form.provinces"
            placeholder="请选择"
            style="width: 105px"
            class="left"
            @change="cityClick"
          >
            <el-option v-for="(item,index) in provinces" :key="index" :label="item" :value="item"></el-option>
          </el-select>
          <el-select v-model="form.city" placeholder="请选择" style="width: 105px">
            <el-option v-for="(item,index) in city" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <div class="d-flex justify-content-end">
            <el-button @click="btnRese">取消</el-button>
            <el-button @click="screen('form')" type="primary">筛选</el-button>
          </div>
        </el-form-item>
      </el-form>
      <!-- 切换页面 -->
      <el-tabs
        v-model="activeName"
        style="padding: 0 0 10px 0"
        type="card"
        @tab-click="handleClick"
      >
        <el-tab-pane label="全部" name="first"></el-tab-pane>
        <el-tab-pane label="待审核" name="second"></el-tab-pane>
        <el-tab-pane label="已审核" name="third"></el-tab-pane>
        <el-tab-pane label="已拒绝" name="fourth"></el-tab-pane>
      </el-tabs>
      <!-- 警告提示 -->
      <el-row class="button">
        <el-alert :title="`数据一共${this.total}条`" type="info" show-icon></el-alert>
      </el-row>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column fixed prop="number" label="试题编号" width="130"></el-table-column>
        <el-table-column prop="subject" label="学科">
          <template slot-scope="{ row }">{{ row.subject }}</template>
        </el-table-column>
        <el-table-column prop="catalog" label="目录">
          <template slot-scope="{ row }">{{ row.catalog }}</template>
        </el-table-column>
        <el-table-column prop="questionType" label="题型">
          <template slot-scope="{ row }">{{ row.questionType | emun(emunData.questionType) }}</template>
        </el-table-column>
        <el-table-column prop="question" label="题干" width="250">
          <template slot-scope="{ row }">{{row.question | html2Text(row.question)}}</template>
        </el-table-column>
        <el-table-column prop="difficulty" label="录入时间" width="220">
          <template
            slot-scope="{ row }"
          >{{ row.difficulty | parseTimeByString("{y}-{m}-{d} {h}:{i}:{s}") }}</template>
        </el-table-column>
        <el-table-column prop="difficulty" label="难度">
          <template slot-scope="{ row }">{{ row.difficulty | emun(emunData.difficulty) }}</template>
        </el-table-column>
        <el-table-column prop="creator" label="录取人" width="130"></el-table-column>
        <el-table-column prop="chkState" label="审核状态">
          <template slot-scope="{ row }">{{ row.chkState | emun(emunData.chkType) }}</template>
        </el-table-column>
        <el-table-column prop="chkRemarks" label="审核意见" width="150">
          <template slot-scope="{ row }">{{ row.chkRemarks }}</template>
        </el-table-column>
        <el-table-column prop="creator" label="审核人" width="130"></el-table-column>
        <el-table-column prop="publishState" label="发布状态">
          <template slot-scope="{ row }">{{ row.publishState | emun(emunData.publishType) }}</template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="190" style="text">
          <template slot-scope="scope">
            <el-button @click="footHandleClick(scope.row)" type="text" size="small">预览</el-button>
            <el-button
              :disabled="scope.row.chkState === 1"
              @click="auditClick(scope.row)"
              type="text"
              size="small"
            >审核</el-button>
            <el-button
              @click="shiti(scope.row.id)"
              :disabled="scope.row.publishState === 1"
              type="text"
              size="small"
            >修改</el-button>
            <el-button
              @click="open(scope.row)"
              type="text"
              size="small"
            >{{scope.row.publishState === 1 ? '上架' : '下架'}}</el-button>
            <el-button @click="del(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block" style="float :right">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pagesize"
          layout=" prev, pager, next,sizes, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <preview :dialog-visible.sync="dialogVisible" :rowDifficultydegree.sync="rowDifficultydegree"></preview>
    <check
      v-if="check"
      :sh.sync="sh"
      :check.sync="check"
      :rowDifficultydegree.sync="rowDifficultydegree"
    ></check>
  </div>
</template>

<script>
import { choice, auditOpinions, choicePublish, remove, detail } from '../../api/hmmm/questions.js'
import { simple } from '@/api/hmmm/subjects.js'
import { list as lists } from '../../api/hmmm/directorys.js'
import { list as title } from '../../api/hmmm/tags.js'
import { list as users } from '../../api/base/users.js'
import { provinces, citys } from '../../api/hmmm/citys.js'
import preview from '../components/questions-preview.vue'
import check from '../components/questions-check.vue'

import {
  chkType,
  publishType,
  difficulty,
  questionType
} from '@/api/hmmm/constants.js'
// console.log('chkType', chkType)
// import { html2Text } from '../../utils/index.js'
export default {
  name: '',
  props: {

  },
  components: { preview, check },
  data () {
    return {
      // 表头数据
      form: {
        twoLevelDirectory: [], // 二级目录
        tags: [], // 标签
        keyword: '',
        subjectName: [],
        difficulty: [], // 试题难度
        direction: [], // 方向
        questionType: [], // 试题类型
        userName: [], // 录入人
        notes: '', // 题目备注
        enterprise: '', // 企业简称
        provinces: [], // 城市
        city: [] // 城市地区
      },
      rules: {
        subjectName: [
          { required: false, message: '请输入添加书名', trigger: 'change' }
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        twoLevelDirectory: [
          { required: true, message: '请选择二级目录', trigger: 'change' }],
        tags: [
          { required: false, message: '请选择标签', trigger: 'change' }],
        keyword: [
          { required: false, message: '请选填写关键字', trigger: 'blur' }],
        difficulty: [
          { required: false, message: '请选试题难度', trigger: 'change' }],
        direction: [
          { required: false, message: '请选试题方向', trigger: 'change' }],
        questionType: [
          { required: false, message: '请选试题类型', trigger: 'change' }],
        userName: [
          { required: false, message: '请选录入人', trigger: 'blur' }],
        notes: [
          { required: false, message: '请输入备注', trigger: 'blur' }],
        enterprise: [
          { required: false, message: '请输入企业简称', trigger: 'blur' }],
        provinces: [
          { required: false, message: '请选择地区', trigger: 'blur' }],
        city: [
          { required: false, message: '请选择城市', trigger: 'blur' }]
      }, // 验证规则
      subjectName: [],
      userName: [],
      city: [], // 城市地区
      provinces: [], // 城市
      values: [{ value: '单选' }, { value: '多选' }, { value: '简单' }],
      value: [{ value: '简单' }, { value: '一般' }, { value: '困难' }],
      directions: [{ value: 'o2o' }, { value: '外包服务' }, { value: '企业服务' }, { value: '互联网金融' }, { value: '企业咨询' }, { value: '互联网' }, { value: '电子商务' }, { value: '其他' }],
      // table切换
      activeName: 'first',
      // 表格数据
      tableData: [],
      // 分页
      page: 1,
      pagesize: 5,
      total: 0,
      b: {},
      // 预览弹出层
      dialogVisible: false,
      // 审核弹出层
      check: false,
      emunData: {
        chkType: chkType,
        publishType: publishType,
        difficulty: difficulty,
        questionType: questionType
      }, // 枚举类数据
      rowDifficultydegree: {}, // 单行数据
      sh: {}, // 审核
      audit: null // 切换审核状态
    }
  },
  filters: {
    emun: (val, array) => {
      const f = array.find(e => e.value === +val)
      // console.log(val, array, f)
      return f ? f.label : '未知'
    }
  },
  created () {
    this.choice()
    this.simple()
  },
  methods: {
    // 删除
    del (val) {
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove({ id: val.id })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      this.choice()
    },
    // 上下架
    open (val) {
      console.log(val.publishState)
      this.$confirm(`你确定要${val.publishState === 1 ? '上架' : '下架'}此题目吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        if (val.publishState === 0) {
          await choicePublish({ id: val.id, publishState: 1 })
        } else {
          await choicePublish({ id: val.id, publishState: 0 })
        }
        this.$message({
          type: 'success',
          message: `${val.publishState === 1 ? '上架' : '下架'}成功!`
        })
        // 成功刷新页面
        this.choice()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    shiti (id) {
      this.$router.push({
        path: '/questions/new',
        query: { id }
      })
      console.log(id)
    },
    async auditClick (row) {
      this.check = true
      this.rowDifficultydegree = row
      const { data } = await auditOpinions({ id: row.id })
      console.log(data)
      this.sh = data[0]
      if (this.sh && this.sh.operation === '通过') {
        this.sh.operation = 1
      } else {
        this.sh.operation = 2
      }
    },
    async footHandleClick (row) {
      this.dialogVisible = true
      const { data } = await detail({ id: row.id })
      console.log(data)
      // console.log(options)
      const isRight = data.options.filter(t => t.isRight === 1)
      const newF = []
      isRight.forEach(ele => {
        newF.push(ele.title)
      })
      console.log(newF)
      this.rowDifficultydegree = data
      // this.rowDifficultydegree = row
    },
    async simple () {
      const { data } = await simple({ page: this.page, pagesize: this.pagesize })
      this.subjectName = data
    }, // 学科列表
    // 分栏
    handleClick (tab, event) {
      if (tab.label === '全部') {
        this.audit = null
        this.choice()
      } else if (tab.label === '待审核') {
        this.audit = 0
        this.choice()
      } else if (tab.label === '已审核') {
        this.audit = 1
        this.choice()
      } else if (tab.label === '已拒绝') {
        this.audit = 2
        this.choice()
      }
    },
    // 分页
    handleSizeChange (val) {
      this.pagesize = val
      this.choice()
      // console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      console.log(val)
      this.page = val
      this.choice()
    },
    // 获取全部数据
    async choice () {
      const { data: page } = await choice({ page: this.page, pagesize: this.pagesize, subjectID: this.b.value, chkState: this.audit })// 精选题库列表
      this.total = page.counts
      this.tableData = page.items
    },
    btnClick2 (a) {
      this.form.twoLevelDirectory.map(item => {
        if (item.directoryName === a) {
          // this.c = item
          console.log(item)
          return item
        }
      })
      console.log(a)
    },
    async btnClick (a) {
      if (a) {
        this.subjectName.map(item => {
          if (item.value === a) {
            this.b = item
            // console.log(item)
            return item
          }
        })
        const { data: items } = await lists({ page: 1, pagesize: 10, subjectID: this.b.value })
        const { data: item } = await title({ page: 1, pagesize: 10, subjectID: this.b.value })
        const { data: { list } } = await users({ page: 1, pagesize: 10, keyword: true })
        this.form.tags = item.items
        console.log(items)
        this.form.twoLevelDirectory = [...items.items]
        this.userName = list
        // console.log('this.userName', this.userName)
        this.provinces = provinces()
      }
    },
    cityClick (city) {
      this.city = citys(this.form.provinces)
    },
    btnRese () {
      // this.$refs.form.resetForm(this.form)
      this.$refs.form.resetFields()
      this.form.city = ''
      // this.simple()
    },
    // 筛选
    screen (screen) {
      this.$refs[screen].validate(async valid => {
        if (valid) {
          this.choice()
        } else {
          alert('请输入筛选条件')
          return false
        }
      })
    }
  }

}
</script>

<style lang="sass" scoped>
.container
  padding: 20px 15px

.d-flex
  display: flex

.flex-grow-1
  flex-grow: 1

.d-flex > .el-select:not(:last-child)
  margin-right: 10px

.el-form
  display: flex
  flex-wrap: wrap

  &-item__label
    font-weight: bold

.el-select
  width: 100%

.el-form--inline .el-form-item
  display: flex
  flex: 25%
  margin-right: 0px

.el-input__inner
  padding: 0 30px
  padding-right: 100px
  // flex: 1;

.el-form-item__content
  flex: 1

.direction
  flex-wrap: nowrap

.left
  margin-right: 5px

.justify-content-end
  position: absolute
  right: -295px

.top
  padding: 25px 3% 15px 0
  display: flex
  justify-content: end
  width: 100%
.block
  padding: 20px 0

.button
  padding-bottom: 20px
.cell
  text-align: center
  .el-dialog
    height: 80vh
</style>
